﻿using CommonBehaviors.Actions;

using TreeSharp;
using Action = TreeSharp.Action;

namespace SPR.Classes.Rogue
{
    using System;
    using System.Linq;
    using global::SPR.Helpers;
    using global::SPR.Lists;
    using Styx.Helpers;
    using Styx.WoWInternals;
    using Styx.WoWInternals.WoWObjects;

    class Subtlety : RotationBase
    {
        
        public override string Name
        {
            get { return "Subtlety Rogue"; }
        }

		// public static readonly HealerBase Healer = HealerBase.Instance;
		
        public override string KeySpell
        {
            get { return "Shadow Dance"; }
        }
        
        // adding some help
        public override string Help
        {
            get
            {
                return "\n" +
                    "----------------------------------------------------------------------\n" +			
					"This Rotation will:\n" +
                    "1. Attempt to evade/escape crowd control with Evasion, Cloak of Shadows, Smoke Bomb, Combat Readiness.\n" +
                    "2. Rotation is set up for Hemorrhage\n" +
                    "3. AutomaticCooldowns has: \n" +
                       "==> UseTrinkets \n" +
                       "==> UseRacials \n" +
                       "==> UseEngineerGloves \n" +
                    "4. Attempt to reduce threat with Feint\n" +
                    "5. Will interupt with Kick\n" +
                    "6. Tricks of the Trade on best target (tank, then class)\n" +
                    "7. Will heal with Recuperate and a Healthstone\n" +
					"NOTE: PvP uses single target rotation - It's not designed for PvP use. \n" +
					"Credits to cowdude\n" +
                    "----------------------------------------------------------------------\n";
            }
        }

        public override Composite SingleRotation
        {
            get
            {
                return new PrioritySelector(
                    /*
                                     // Trinkets & Cooldowns
                    new Decorator(
                        ret => Units.IsTargetWorthy(Me.CurrentTarget),
                            new PrioritySelector(
                                    Item.UseTrinkets(),
                                    Spells.UseRacials(),
                                    Buff.CastBuff("Lifeblood", ret => true, "Lifeblood"), // Thanks Kink
                                    Items.UseEngineerGloves(ret => true, "Engineer's gloves"))),
                    
                    Spells.CastInterupt("Kick",                     ret => true, "Kick"),
                    Spells.CastSpell("Redirect",                    ret => Me.RawComboPoints > 0 && Me.ComboPoints < 1, "Redirect"),
                    Items.RunMacroText("/cancelaura Blade Flurry",  ret => Unit.EnemyUnits.Count() < 2 && Buff.PlayerHasBuff("Blade Flurry"), "[CancelAura] Blade Flurry"),
                    Spells.CastSelfSpell("Blade Flurry",            ret => Unit.EnemyUnits.Count() >= 2, "Blade Flurry"),
                    Spells.CastAreaSpell("Fan of Knives", 8, false, 6, 0.0, 0.0, ret => Me.CurrentEnergy > 85, "Fan of Knives"),
                    Spells.CastSpellonUnit("Tricks of the Trade", u => Units.BestTricksTarget, ret => true, "Tricks of the Trade"),
                    Spells.CastSpell("Expose Armor",                ret => Me.ComboPoints == 5 && Units.IsTargetWorthy(Me.CurrentTarget) && !Buffs.UnitHasArmorReductionDebuff(Me.CurrentTarget), "Expose Armor"),
                    Spells.CastSelfSpell("Slice and Dice",          ret => Buff.PlayerBuffTimeLeft("Slice and Dice").TotalSeconds < 2, "Slice and Dice"),
                    Spells.CastSelfSpell("Killing Spree",           ret => Me.CurrentEnergy < 35 && Buff.PlayerBuffTimeLeft("Slice and Dice").TotalSeconds > 4 && !Buff.PlayerHasBuff("Adrenaline Rush") && SPR.Instance.AutoManageCooldowns, "Killing Spree"),
                    Spells.CastSelfSpell("Adrenaline Rush",         ret => Me.CurrentEnergy < 35 && Units.IsTargetWorthy(Me.CurrentTarget), "Vanish"),
                    Spells.CastSpell("Eviscerate",                  ret => Me.ComboPoints == 5 && (Buff.PlayerHasBuff("Moderate Insight") || Buff.PlayerHasBuff("Deep Insight")), "Eviscerate & Moderate Insight or Deep Insight"),
                    Spells.CastSpell("Rupture",                     ret => Me.ComboPoints == 5 && !Buff.TargetHasDebuff("Rupture") && Buffs.UnitHasBleedDamageDebuff(Me.CurrentTarget), "Rupture"),
                    Spells.CastSpell("Eviscerate",                  ret => Me.ComboPoints == 5, "Eviscerate"),
                    Spells.CastSpell("Revealing Strike",            ret => Me.ComboPoints == 4 && !Buff.TargetHasDebuff("Revealing Strike"), "Revealing Strike"),
                    Spells.CastSpell("Sinister Strike",             ret => Me.ComboPoints < 5, "Sinister Strike")
                    */
                    // Extra button automation
                    Items.RunMacroText("/click ExtraActionButton1", ret => (Units.IsFadingLight() || Units.IsShrapnel() || Units.IsHourofTwilight()) && SPR.Instance.AutoExtraActionButton, "[Push Button] ExtraActionButton1"),
                    Spells.CastSelfSpell("Feint",                   ret => Units.IsMorchokStomp(), "Feint"),
                    Spells.CastSpell("Redirect",                    ret => Me.RawComboPoints > 0 && Me.ComboPoints < 1, "Redirect"),
                    //Items.RunMacroText("/cast [@focus] Tricks of the Trade", ret => Me.FocusedUnit != null, "ToTT"),
                    Spells.CastSpellonUnit("Tricks of the Trade",   u => Units.BestTricksTarget, ret => true, "Tricks of the Trade"),
                    new Decorator(
                        ret => !Buff.PlayerHasActiveBuff("Stealth") && !Buff.PlayerHasActiveBuff("Vanish"),
                        new PrioritySelector(
                            Spells.CastSelfSpell("Slice and Dice",  ret => Me.ComboPoints == 5 && Buff.PlayerBuffTimeLeft("Slice and Dice").TotalSeconds < 2, "Slice and Dice"),
                            Spells.CastSelfSpell("Recuperate",      ret => Me.ComboPoints == 5 && Buff.PlayerBuffTimeLeft("Recuperate").TotalSeconds < 2, "Recuperate"),
                            Spells.CastSpell("Rupture",             ret => Me.ComboPoints == 5 && !Buff.TargetHasDebuff("Rupture") && Buffs.UnitHasBleedDamageDebuff(Me.CurrentTarget), "Rupture"),
                            Spells.CastSpell("Eviscerate",          ret => Me.ComboPoints == 5, "Eviscerate"),
                            new Decorator(
                                ret => !Buff.PlayerHasActiveBuff("Shadow Dance"),
                                new PrioritySelector(
                                    //Spells.CastSpell("Hemorrhage",      ret => //currentEnergy() >= 29 && 
                                    //    !Me.CurrentTarget.HasAura(89775), "Hemorrhage Bleed"),
                                    Spells.CastSpell("Hemorrhage", ret => !Buffs.UnitHasBleedDamageDebuff(Me.CurrentTarget), "Hemorrhage debuff"),
                                    Spells.CastSpell("Backstab",        ret => //currentEnergy() >= 40 && 
                                        isBehindTarget() && SPR.Instance.AutoCPG, "Backstab"),
                                    Spells.CastSpell("Hemorrhage",      ret => //currentEnergy() >= 29 && 
                                        !isBehindTarget() && SPR.Instance.AutoCPG, "Hemorrhage")
                                )
                            )
                        )
                    ),
                    new Decorator(
                        ret => Buff.PlayerHasActiveBuff("Stealth") || Buff.PlayerHasActiveBuff("Vanish") || Buff.PlayerHasActiveBuff("Shadow Dance"),
                        new PrioritySelector(
                            Spells.CastSpell("Premeditation", ret => Me.ComboPoints <= 3, "Premeditation"),
                            Spells.CastSpell("Ambush", ret => true//currentEnergy() >= 40
                            , "Ambush")
                        )
                    )
                );
            }
        }

        public override Composite Medic
        {
            get
            {
                return new PrioritySelector(
                    Spells.CastSelfSpell("Feint", ret => Me.CurrentTarget.ThreatInfo.RawPercent > 80 || Units.IsMorchokStomp(), "Feint")
                );
            }
        }

        public override Composite PreCombat
        {
            get
            {
                return new Decorator(
                            ret => !Me.Mounted && !Me.Dead && !Me.Combat && !Me.IsFlying && !Me.IsOnTransport && !Me.HasAura("Food") && !Me.HasAura("Drink"),
                            new PrioritySelector(
                                new Decorator(
                                    ret => true,
                                        new PrioritySelector(
                                             new ActionAlwaysSucceed()))));
            }
        }

        public override Composite PVPRotation
        {
            get
            {
                return SingleRotation;
            }
        }

        public override Composite PVERotation
        {
            get
            {
                return SingleRotation;
            }
        }

        #region InClassHelper
        private bool isBehindTarget()
        {
            if (Me.CurrentTarget.Name == "Burning Tendons"
                || Me.CurrentTarget.Name == "Wing Tentacle"
                || Me.CurrentTarget.Name == "Arm tentacle"
                || Me.CurrentTarget.Name == "Frozen Binding Crystal")
            {
                return true;
            }
            else
            {
                return Me.CurrentTarget.MeIsBehind;
            }
        }

        private void ToTT()
        {
            if (Me.FocusedUnit != null)
            {
                if (Me.FocusedUnit.IsFriendly && Me.FocusedUnit.IsAlive)
                {
                    Lua.DoString(String.Format("RunMacroText(\"/cast [@focus] Tricks of the Trade\")"));
                }
            }
        }

        private int currentEnergy()
        {
            return Lua.GetReturnVal<int>("return UnitMana(\"player\");", 0);
        }

        private bool TargetHasBleed()
        {
            return (Me.CurrentTarget.HasAura("Mangle") ||
                Me.CurrentTarget.HasAura("Hemorrhage") ||
                Me.CurrentTarget.HasAura("Tendon Rip") ||
                Me.CurrentTarget.HasAura("Stampede") ||
                Me.CurrentTarget.HasAura("Gore") ||
                Me.CurrentTarget.HasAura("Trauma"));
        }
        #endregion
       
    }
}